{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7b116b36",
   "metadata": {},
   "source": [
    "# 1.音频录制\n",
    "\n",
    "* 安装下载如下3个模块；pip install模块名称(pip install SpeechRecognition)\n",
    "> 1. SpeechRecognition\n",
    "> 2. Pyaudio\n",
    "> 3. pydub"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "eed2584e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13908e87",
   "metadata": {},
   "source": [
    "# 2.语音识别\n",
    "> 1. 百度API：百度的AI账户--（Key,secret）\n",
    "> 2.I/O:input:音频文件(pcm/wav/amr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9eee8050",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdasr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b6c2dc1f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"refresh_token\":\"25.6be994c2ca7b1ff21804767afd07f142.315360000.2002162292.282335-33415705\",\"expires_in\":2592000,\"session_key\":\"9mzdXRCmkZ+aIVZm\\/ENZOO+cLarJN9tGeAu7\\/5XjwgGMNV3GhqQI226CQYyPtxv3K15f+xKxdkpUH+boBkwNeYYXvjY\\/Tw==\",\"access_token\":\"24.fd5fea78baafde61db145bc6f170dea9.2592000.1689394292.282335-33415705\",\"scope\":\"audio_voice_assistant_get brain_enhanced_asr audio_tts_post brain_speech_realtime public brain_all_scope brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\\u6743\\u9650 vis-classify_flower lpq_\\u5f00\\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\\u5f00\\u653eScope vis-ocr_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 idl-video_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx\",\"session_secret\":\"11706246e14becaf6543ed183fa42597\"}\n",
      "\n",
      "{'refresh_token': '25.6be994c2ca7b1ff21804767afd07f142.315360000.2002162292.282335-33415705', 'expires_in': 2592000, 'session_key': '9mzdXRCmkZ+aIVZm/ENZOO+cLarJN9tGeAu7/5XjwgGMNV3GhqQI226CQYyPtxv3K15f+xKxdkpUH+boBkwNeYYXvjY/Tw==', 'access_token': '24.fd5fea78baafde61db145bc6f170dea9.2592000.1689394292.282335-33415705', 'scope': 'audio_voice_assistant_get brain_enhanced_asr audio_tts_post brain_speech_realtime public brain_all_scope brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': '11706246e14becaf6543ed183fa42597'}\n",
      "audio_voice_assistant_get\n",
      "SUCCESS WITH TOKEN: 24.fd5fea78baafde61db145bc6f170dea9.2592000.1689394292.282335-33415705  EXPIRES IN SECONDS: 2592000\n",
      "Request time cost 0.658836\n",
      "{\"corpus_no\":\"7244760683935381959\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"你好，北京科技馆。\"],\"sn\":\"41771694041686802293\"}\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'你好，北京科技馆。'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bdasr_result = eval(bdasr.asr('1.wav'))['result'][0]\n",
    "bdasr_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "671572cc",
   "metadata": {},
   "source": [
    "# 3.连接自动回复字典（openai-chatgpt）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "26584600",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa = {\n",
    "    \"你好，北京科技馆。\":\"中国科学技术馆，位于北京市朝阳区北辰东路5号\",\n",
    "   \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3168dd44",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'中国科学技术馆，位于北京市朝阳区北辰东路5号'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qa_result = qa.get(bdasr_result)\n",
    "qa_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1631607",
   "metadata": {},
   "source": [
    "# 4.语音合成\n",
    "> 1. 百度API：百度的AI账号--（Key,secret）\n",
    "> 2. I/O:input:文本"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b44ea339",
   "metadata": {},
   "source": [
    "# 5.播放音频"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0d9f7dca",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\ProgramData\\Anaconda3\\lib\\site-packages\\pydub\\utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work\n",
      "  warn(\"Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work\", RuntimeWarning)\n"
     ]
    }
   ],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c232303f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b9c590d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
